<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org"
      xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout"
      layout:decorate="~{layout}">

<head>
    <title>药品列表</title>
</head>

<body>
<div layout:fragment="content">
    <!-- 原有内容保持不变 -->
    <div class="mt-4">
        <!-- 消息提示 -->
        <div th:if="${successMessage}" class="alert alert-success" th:text="${successMessage}"></div>
        <div th:if="${errorMessage}" class="alert alert-danger" th:text="${errorMessage}"></div>
        <form th:action="@{/api/drugs/new}" method="post"
            th:if="${session.user.hasRole(4) || session.user.hasRole(8)}">
            <button type="submit" class="btn btn-sm btn-info">新增药品</button>
        </form>
        <table class="table table-hover">
            <thead class="table-dark">
            <tr>
                <th>名称</th>
                <th>生产商</th>
                <th>价格</th>
                <th>库存</th>
                <th>操作</th>
            </tr>
            </thead>
            <tbody>
            <tr th:each="drug : ${drugs}">
                <td th:text="${drug.name}"></td>
                <td th:text="${drug.manufacturer}"></td>
                <td th:text="'￥' + ${#numbers.formatDecimal(drug.price, 1, 2)}"></td>
                <td th:text="${drug.stock}"></td>
                <td>
                    <form th:action="@{/api/drugs/detail}" method="post" class="d-inline">
                        <input type="hidden" name="did" th:value="${drug.id}">
                        <button type="submit" class="btn btn-sm btn-info">详情</button>
                    </form>
                    <form th:action="@{/api/drugs/edit}" method="post" class="d-inline"
                          th:if="${session.user.hasRole(4) || session.user.hasRole(8)}">
                        <input type="hidden" name="did" th:value="${drug.id}">
                        <button type="submit" class="btn btn-sm btn-warning">编辑</button>
                    </form>
                    <form th:action="@{/api/drugs/delete}" method="post" class="d-inline"
                          th:if="${session.user.hasRole(4) || session.user.hasRole(8)}">
                        <input type="hidden" name="did" th:value="${drug.id}">
                        <button type="submit" class="btn btn-sm btn-danger"
                                onclick="return confirm('确定删除该药品吗？')">删除</button>
                    </form>
                </td>
            </tr>
            </tbody>
        </table>
    </div>
</div>
</body>
</html>